package name.schooley.umarks.domain;

import java.security.SecureRandom;
import java.util.List;
import java.util.Random;
import name.schooley.umarks.domain.Bookmark;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

privileged aspect BookmarkDataOnDemand_Roo_DataOnDemand {
    
    declare @type: BookmarkDataOnDemand: @Component;
    
    private Random BookmarkDataOnDemand.rnd = new SecureRandom();
    
    private List<Bookmark> BookmarkDataOnDemand.data;
    
    public Bookmark BookmarkDataOnDemand.getNewTransientBookmark(int index) {
        name.schooley.umarks.domain.Bookmark obj = new name.schooley.umarks.domain.Bookmark();
        obj.setDescription("description_" + index);
        obj.setName("name_" + index);
        obj.setUrl("url_" + index);
        return obj;
    }
    
    public Bookmark BookmarkDataOnDemand.getSpecificBookmark(int index) {
        init();
        if (index < 0) index = 0;
        if (index > (data.size()-1)) index = data.size() - 1;
        Bookmark obj = data.get(index);
        return Bookmark.findBookmark(obj.getId());
    }
    
    public Bookmark BookmarkDataOnDemand.getRandomBookmark() {
        init();
        Bookmark obj = data.get(rnd.nextInt(data.size()));
        return Bookmark.findBookmark(obj.getId());
    }
    
    public boolean BookmarkDataOnDemand.modifyBookmark(Bookmark obj) {
        return false;
    }
    
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void BookmarkDataOnDemand.init() {
        if (data != null) {
            return;
        }
        
        data = name.schooley.umarks.domain.Bookmark.findBookmarkEntries(0, 10);
        if (data == null) throw new IllegalStateException("Find entries implementation for 'Bookmark' illegally returned null");
        if (data.size() > 0) {
            return;
        }
        
        data = new java.util.ArrayList<name.schooley.umarks.domain.Bookmark>();
        for (int i = 0; i < 10; i++) {
            name.schooley.umarks.domain.Bookmark obj = getNewTransientBookmark(i);
            obj.persist();
            data.add(obj);
        }
    }
    
}
